/*
 * CircleSet.java
 *
 * Created on November 7, 2006, 1:10 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.circle;

import edu.umn.cs5115.scheduler.framework.Document;
import edu.umn.cs5115.scheduler.framework.ManagedData;
import edu.umn.cs5115.scheduler.framework.ManagedSet;

/**
 * A set of circles that contains between 1 and 10 circles.  Shows how to use
 * the ManagedSet class.
 * @author grant
 */
public class CircleSet extends ManagedData
{
    /** Other classes can register on this key to receive notifications of when
     * the set of circles changes.  The method that will be called is the set
     * mutation method, not the standard value change method.
     * @see edu.umn.cs5115.scheduler.framework.KeyValueObserver#valueChanged(KeyValueCoding, String, KeyValueObserver.SetMutationType, Set)
     */
    public final static String CIRCLES_KEY = "circles";
    
    /**
     * We hang onto this for convenience.  It's stored in the ManagedData's 
     * store as well (as all persistent information should be), but this way we
     * don't have to fetch it using getValueForKey every time we want to work
     * with it.  ManagedSets automatically notify observers of changes that are
     * made to them and register for undo.  Because we store this in the 
     * ManagedData's store as well under the CIRCLES_KEY, other classes can 
     * listen to changes in this set just by adding themselves as a listener
     * to a CircleSet, rather than to the ManagedSet circles.
     */
    private ManagedSet circles;

    public CircleSet(Document document, Circle firstCircle)
    {
        // initialize the ManagedData with the document.  This allows for automatic
        // undo registration whenever data in the class changes.
        super(document);
        
        // initialize the managed set we will be using to store circles.
        circles = new ManagedSet(document);
        
        // add the first circle to the set
        circles.add(firstCircle);
        
        // store this in the ManagedData store.  Although circles is a class
        // variable, and we'll be able to access it directly, saving it in
        // the store this way allows the KeyValueCodingBase to forward 
        // notifications sent by the circle set to observers of the CIRCLES_KEY.
        setPrimitiveValueForKey(CIRCLES_KEY, circles);
    }
    
    /**
     * Adds a circle to the list of circles.
     * @param circle The circle to add to the set.
     */
    public boolean addCircle(Circle circle)
    {
        if (circles.size() < 10)
            return circles.add(circle);
        else
            return false;
    }
    
    /**
     * Removes a circle from the list of circles.
     * @param circle The circle to remove from the set.
     */
    public boolean removeCircle(Circle circle)
    {
        if (circles.size() > 1)
            return circles.remove(circle);
        else
            return false;
    }
}
